<?php
/*
	$m_table
	$m_columns		chua danh sach cac cot se duoc hien thi. neu khong co phan tu nao thi se hien thi toan bo
						key la ten cot trong database, value la chuoi tieu de se hien thi ra man hinh
	$db				su dung doi tuong nay de thao tac voi co so du lieu, truoc khi goi ham show() phai goi ham from() 
						cua bien $db chon bang muon hien thi. VD tren view goi: 
							$CI->table_viewer->db->from('ten_bang');
							$CI->table_viewer->db->show();
	$m_keyField		khoa chinh cua bang, gia tri cua khoa se duoc gui nhu tham so khi chon chuc nang sua hoac xoa
						neu bien nay khong duoc gan, mac dinh se la truong dau tien xuat hien trong bang
	$m_editURL		duong link den Controller thuc hien chuc nang sua
	$m_deleteURL	duong link den Controller thuc hien chuc nang xoa
	$m_param		tham so nay se luon duoc truyen den Controller khi chon sua hoac xoa, no se duoc noi 
					vao sau duong link $m_editURL va $m_deleteURL va sau do gia tri tuong ung voi $m_keyField 
					se duoc noi them vao
	$m_enableEdit	cho phep sua du lieu, mac dinh la TRUE
	$m_enableDelete	cho phep cot xoa du lieu, mac dinh la TRUE
	$checkboxColumns	mang chua cac cot la checkbox, key la ten cot, value la duong link khi kich vao checkbox,
							neu khong co link thi cho chuoi rong vao
	$m_imageColumns
	$m_thumbnailColumns
	$m_comboColumns
	$m_searchColumns
*/
class Table_viewer
{
	var $_CI;
	var $m_columns = array();
	var $db;
//	var $m_cssfile = 'css/admin/styles.css';
	var $m_headerCss = 'title_table';
	var $m_cellCss = 'bodytext_table';
	var $m_editURL = 'admin/quanly/sua';
	var $m_deleteURL = 'admin/quanly/xoa';
	var $m_keyField = NULL;
	var $m_param = NULL;
	var $m_rowheight = 20;
	var $m_maxlength = 1000;
	var $m_enableDelete = TRUE;
	var $m_enableEdit = TRUE;
	var $m_imageColumns = array();
	var $m_comboColumns = array();
	var $m_thumbnailColumns = array();
	var $m_editableColumns = array('Tendanhmucsanpham'=>'Tendanhmucsanpham','Sothutu'=>'Sothutu');
	var $m_table = '';
	var $m_searchColumns = array();
	
	function Table_viewer()
	{
		$this->_CI = &get_instance();
		$this->_CI->load->database();
		$this->_CI->load->helper('url');
		$this->db = &$this->_CI->db;
	}
	function show($return = FALSE)
	{
		$jsparams1 = '';
		$jsparams2 = '';
		$query = $this->db->query('show columns from ' . $this->m_table);
		$datetimeColumns = array();
		foreach($query->result() as $r)
		{
			if(isset($this->m_editableColumns[$r->Field]))
			{
				if($r->Null == 'NO')
				{
					$jsparams1 .= '\'txt_' . $this->m_table . '/' . $r->Field . '\',';
					$jsparams2 .= '\'required\',';
				}
				if(strpos($r->Type, 'int') === 0 || strpos($r->Type, 'bigint') === 0)
				{
					$jsparams1 .= '\'txt_' . $this->m_table . '/' . $r->Field . '\',';
					$jsparams2 .= '\'numeric\',';
				}
			}
			if($r->Type == 'datetime')
				$datetimeColumns[$r->Field] = $r;
		}
		$jsparams1 = substr($jsparams1, 0, strlen($jsparams1) - 1);
		$jsparams2 = substr($jsparams2, 0, strlen($jsparams2) - 1);
		if($this->_CI->input->post('save') != FALSE)
		{
			if($this->m_keyField == NULL)
			{
				foreach($query->result() as $r)
				{
					$this->m_keyField = $r->Field;
					break;
				}
			}
			
			
			
			foreach($_POST as $key=>$value)
			{
				if(strpos($key, 'txt_') === 0)
				{
					$temp = split('/', substr($key, 4));
//					echo 'update ' . $temp[0] . ' set ' . $temp[1] . ' = \'' . $value . '\' where ' . $this->m_keyField . ' = \'' . $temp[2] . '\'';
					$this->db->query('update ' . $temp[0] . ' set ' . $temp[1] . ' = \'' . $value . '\' where ' . $this->m_keyField . ' = \'' . $temp[2] . '\'');
				}
			}
		}
		
		$ret = '';
		$query = $this->db->get();
		$ret .= '<base href=' . base_url() . '/>';
//		$ret .= '<link href="' . $this->m_cssfile . '" rel="stylesheet" type="text/css" />';
		$ret .= '<script type="text/javascript" src="javascript/admin/script.js"></script>';
		$ret .= "\r\n";
		$ret .= '<form method="post">';
		foreach($this->m_searchColumns as $key=>$value)
		{
			$ret .= $this->m_columns[$key] . ': ';
			if($this->_CI->session->userdata('search/'.$this->m_table.'/'.$key))
				$ret .= '<input type="text" name="search/'.$this->m_table.'/'.$key.'" value="'.$this->_CI->session->userdata('search/'.$this->m_table.'/'.$key).'"/><br/>';
			else
				$ret .= '<input type="text" name="search/'.$this->m_table.'/'.$key.'"/><br/>';
		}
//		$ret .= 'Chuỗi tìm kiếm: <input type="text" name="chuoitimkiem/'.$this->m_table.'" value="'.$this->_CI->session->userdata('chuoitimkiem/'.$this->m_table).'"/><br/>';
/////////////////////////// loc combobox
		if(count($this->m_comboColumns) > 0)
		{
			foreach($this->m_comboColumns as $key=>$value)
			{
				$ret .= $this->m_columns[$key] . ': ';
				$data = array();
				$data[-1] = 'Tất cả';
				foreach($value as $key2=>$value2)
				{
					$data[$key2] = $value2;
				}
				if($this->_CI->session->userdata('filter/' . $this->m_table . '/' . $key) != NULL)
					$ret .= form_dropdown('dropdown/' . $this->m_table . '/' . $key, $data, $this->_CI->session->userdata('filter/' . $this->m_table . '/' . $key));
				else
					$ret .= form_dropdown('dropdown/' . $this->m_table . '/' . $key, $data);
			}
		}
		/*if(count($datetimeColumns) > 0)
		{
			foreach($datetimeColumns as $key=>$value)
			{
				$ret .= '<br/>';
				if($this->_CI->session->userdata('check/'.$this->m_table.'/'.$key))
					$ret .= '<input type="checkbox" name="check/'.$this->m_table.'/'.$key.'" checked/>';
				else
					$ret .= '<input type="checkbox" name="check/'.$this->m_table.'/'.$key.'"/>';
				$ret .= $this->m_columns[$key] . ' từ: ';
				if($this->_CI->session->userdata('datefrom/'.$this->m_table.'/'.$key))
				{
					$ret .= form_date(array('name'=>'datefrom/'.$this->m_table.'/'.$key, 'value'=>$this->_CI->session->userdata('datefrom/'.$this->m_table.'/'.$key)));
				}
				else
					$ret .= form_date(array('name'=>'datefrom/'.$this->m_table.'/'.$key));
				$ret .= ' đến: ';
				if($this->_CI->session->userdata('dateto/'.$this->m_table.'/'.$key))
					$ret .= form_date(array('name'=>'dateto/'.$this->m_table.'/'.$key, 'value'=>$this->_CI->session->userdata('dateto/'.$this->m_table.'/'.$key)));
				else
					$ret .= form_date(array('name'=>'dateto/'.$this->m_table.'/'.$key));
			}
		}*/
		$ret .= '<br/>';
		$ret .= '<input type="submit" name="filter" value="Lọc">';
		$ret .= '</form>';
//////////////////////////// ////////////////////
		$ret .= '<form method="post" onsubmit="return Validate(new Array(' . $jsparams1.'),new Array(' . $jsparams2 . '));">';
		$ret .= '<table cellspacing="0" border="1" bordercolor="#A6D9FF" style="border-collapse:collapse;">';

		{
			if(count($this->m_columns) == 0)
			{
				foreach($query->list_fields() as $field)
				{
					$this->m_columns[$field] = $field;
				}
			}
			
			$ret .= '<tr>';
			foreach($this->m_columns as $key=>$field)
			{
				$ret .= '<th height='.$this->m_rowheight.' class="'.$this->m_headerCss.'" scope="col"><div align="center"><div align="left">';
				$ret .= "$field";
				if(isset($this->m_editableColumns[$key]))
				{
//					$ret .= '<input type="button" name="lkj" value="jlk" onclick="Editmode(\'lbl_'.$key.'\',\'txt_\')"/>';
					$ret .= '<a href="#" onclick="Editmode(\'lbl_'.$key.'\',\'txt_'.$this->m_table.'/'.$key.'\');return false;"><img src="image/icon/edit.png" border="0" /></a>';
																						 
				}
				$ret .= '</div></div></th>';
			}
			if($this->m_enableEdit)
				$ret .= '<th width="23" height="'.$this->m_rowheight.'" class="'.$this->m_headerCss.'" scope="col"><div align="left">Sửa</div></th>';
			if($this->m_enableDelete)
				$ret .= '<th width="23" height="'.$this->m_rowheight.'" class="'.$this->m_headerCss.'" scope="col"><div align="left">Xóa</div></th>';
//			echo '<th width="23" height="40" class="'.$this->m_headerCss.'" scope="col"><div align="left"></div></th>';
			$ret .= '</tr>';
			$ret .= "\r\n";
			$index = 0;
			foreach($query->result() as $row)
			{
				$id;
				if($this->m_keyField !== NULL)
				{
					$key = $this->m_keyField;
					$id = $row->$key;
				}
				else
				{
					foreach($this->m_columns as $key=>$value)
					{
						$id = $row->$key;
						break;
					}
				}
				$index++;
				if($index % 2 == 0)
					$ret .= '<tr bgcolor="#EAEAEA" >';
				else
					$ret .= '<tr bgcolor="#ffffff">';
				foreach($this->m_columns as $key=>$value)
				{
					$ret .= '<td height="'.$this->m_rowheight.'" class="'.$this->m_cellCss.'"><div align="left" title="'.$row->$key.'">';
					if(isset($this->m_checkboxColumns) && isset($this->m_checkboxColumns[$key]))
					{
						if($this->m_checkboxColumns[$key])
						{
							if($row->$key)
								$ret .= '<div align="center"><a href="'.site_url($this->m_checkboxColumns[$key]).'/'.$this->m_param.'/'.$id.'/'.$key.'/1'.'"><img border="0" width="16" height="16" src="image/icon/tick_16.png"/></a><div>';
							else
								$ret .= '<div align="center"><a href="'.site_url($this->m_checkboxColumns[$key]).'/'.$this->m_param.'/'.$id.'/'.$key.'/0'.'"><img border="0" width="16" height="16" src="image/icon/negative.png"/></div>';
						}
						else
						{
							if($row->$key)
								$ret .= '<a><img border="0" width="16" height="16" src="image/icon/tick_16.png"/></a>';
							else
								$ret .= '<a><img border="0" width="16" height="16" src="image/icon/negative.png"/>';
						}
					}
					else if(isset($this->m_imageColumns[$key]) || isset($this->m_thumbnailColumns[$key]))
					{
						$ret .= '<img width="18" height="18" src="'.$row->$key.'" onmouseover="showtrail(200, 200,\''.$row->$key.'\');" onmouseout="hidetrail();"/>';
					}
					else if(isset($this->m_comboColumns[$key]))
					{
						if(isset($this->m_comboColumns[$key][$row->$key]))
							$ret .= $this->m_comboColumns[$key][$row->$key];
					}
					else if(isset($datetimeColumns[$key]))
					{
						if($row->$key)
						{
							$temp1 = split(' ', $row->$key);
							$temp2 = split('-', $temp1[0]);
							$ret .= $temp2[2] . '/' . $temp2[1] . '/' . $temp2[0];
						}
					}
					else
					{
						if(strlen($row->$key) < $this->m_maxlength)
						{
							$ret .= '<a name="lbl_' . $key . '">';
							$ret .= $row->$key;
							$ret .= '</a>';
						}
						else
						{
							$ret .= '<a name="lbl_' . $key . '">';
							$ret .= substr($row->$key, 0, $this->m_maxlength) . '...';
							$ret .= '</a>';
						}
						
						if(isset($this->m_editableColumns[$key]))
							$ret .= '<input type="text" name="txt_'.$this->m_table.'/'.$key.'/'.$id.'"  value="'.$row->$key.'" size="15" style="display:none"/>';
					}
					$ret .= '</div></td>';
				}
				
				
				
				if($this->m_enableEdit)
					$ret .= '<td width="37" height="'.$this->m_rowheight.'" class="'.$this->m_cellCss.'"><div align="center"><a href='.site_url($this->m_editURL.'/'.$this->m_param.'/'.$id).'><img border="0" src="image/icon/edit.png" alt="edit" width="18" height="18" /></a></div></td>';
				if($this->m_enableDelete && $this->m_table!=='tbltopmenu' || $id!=='1' && $id!=='2' && $id!=='3' && $id!=='4' && $id!=='5')
					$ret .= '<td height="'.$this->m_rowheight.'" class="'.$this->m_cellCss.'"><div align="center"><a class="deleteButton" id='.$id.' href='.site_url($this->m_deleteURL.'/'.$this->m_param.'/'.$id).'><img border="0" src="image/icon/remove.png" alt="remove" width="18" height="18" /></a></div></td>';
//				echo '<td height="40" class="'.$this->m_cellCss.'"><div align="center"><label></label><input type="checkbox" name="checkbox" id="checkbox" /></div></td>';
				$ret .= '</tr>';
				$ret .= "\r\n";
			}
			
		}
		
		$ret .= '</tr>';
		$ret .= '</table>';
		$ret .= '<br>';
		$ret .= '<input type="submit" name="save" value="Đồng ý"/>';
		$ret .= '<input type="reset" name="reset" value="Làm lại"/>';
		$ret .= '</form>';
		
		if($return)
			return $ret;
		else
			echo $ret;
	}
}
?>